import QtQuick 2.7
import QtQuick.Controls 2.1
import QtQuick.Layouts 1.0
import QtQuick.Window 2.2
import QtQuick.Controls.Material 2.1
import Han.Login 1.0
import Han.Storage 1.0
ApplicationWindow {
    id:window
    visible: true
    property real stuid; //全局 学生id
     property real  dp:Screen.pixelDensity.toFixed(2)  //获取dp 精确小数点2位数
    width: 1040
    height: 697
    title: qsTr("学生管理系统v1.0")
    Material.theme: Material.Light
    Material.accent: "#282A3D"
    Image {
        id: name
        source: "qrc:/background/background/background.jpg"
    }

 //  Material.accent: Material.Teal
    ///////一系列网络接口
    Login{
        id:loginid
        onReceived:{
            var oJson=JSON.parse(str);
            var loginstatus=oJson.code;
            if(loginstatus == 1)
            {
                dialog.visible=true;
                dialog.title="登录成功"
                dialogcontent.text="登录成功"
                indicator.running=false;
                window.stuid=oJson.stuid;
                stackview.push("qrc:/mainmenu.qml");
            }else{
                dialog.visible=true;
                dialog.title="登录失败"
                dialogcontent.text="登录失败"
                indicator.visible=false
            }
        }
    }
    Storage{
        id:storage
    }
    ///当前页面加载完毕
    Component.onCompleted: {
        if(storage.isSaved()==true)
        {
            textName.text=storage.readName();
            textpwd.text=storage.readPwd();
        }else{
            radiorem.checked=false;
        }
    }
    ////网络接口结束
    BusyIndicator
    {
        id:indicator
        running: false
        anchors.centerIn: parent;
        z:5//最顶层
    }

    Dialog {
        id: dialog
        width:window.width/3
        leftMargin: window.width/3
        topMargin: window.height/3
        modal: true
        title: qsTr("登录失败")
        visible: false
        Label {
            id:dialogcontent
            text: "密码错误"
        }

        standardButtons: Dialog.Ok
    }
    StackView
    {
        anchors.fill: parent
        id:stackview
        initialItem: Item{
            anchors.fill: parent
            Rectangle{
                id:rectForm
                width:dp*100
                height:dp*100
                color:"white"
                anchors.centerIn: parent;
                opacity:0.3
                z:1
            }
            //Rectangle form这个rectangle是中间白框
            //为了不被opacity 代码没有放在里面，而是定位定进去，而且设置z
                Label{
                    text:"登录学生管理系统"
                    id:textTitle
                    font.pixelSize: dp*7
                    anchors.top:rectForm.top
                    anchors.topMargin: dp*10
                    anchors.horizontalCenter: rectForm.horizontalCenter
                    z:2

                }

                TextField{
                    id:textName
                     anchors.top:textTitle.bottom
                     anchors.topMargin: dp*12
                     anchors.horizontalCenter: rectForm.horizontalCenter
                      width:textTitle.width
                   //   anchors.left:rectForm.left
                   //  anchors.leftMargin:rectForm.width/3
                   //  anchors.right: parent.right
                    // anchors.rightMargin:rectForm.width/3
                     z:2
                }
                TextField{
                    id:textpwd
                    echoMode:TextInput.Password
                    anchors.top:textName.bottom
                    anchors.topMargin: dp*4
                     anchors.horizontalCenter: rectForm.horizontalCenter
                    width:textTitle.width
                     z:2
                }
                CheckBox{
                    id:radiorem
                    checked: true
                    text: qsTr("记住密码")
                    anchors.top:textpwd.bottom
                    anchors.topMargin: dp*1
                    anchors.horizontalCenter:rectForm.horizontalCenter
                    width:textTitle.width
                     z:2
                }
                Button{
                    text:"登录"
                     anchors.top:radiorem.bottom
                    anchors.topMargin: dp*1
                    anchors.horizontalCenter:rectForm.horizontalCenter
                    width:textTitle.width
                    Material.background: "#104579"
                    Material.foreground: "#FFFFFF"
                    onClicked: {
                        indicator.running=true;
                        if(radiorem.checked==true)
                            storage.SaveNamePwd(textName.text,textpwd.text);
                        else
                            storage.Clear();

                        loginid.doLogin(textName.text,textpwd.text);
                    }
                     z:2
                }
                Image{
                   // text:"用户名:"
                 //   font.pixelSize: dp*5
                    width:10*dp;
                    height:10*dp
                    source: "qrc:/background/background/head.png"
                     anchors.top:textName.top
                     anchors.left:rectForm.left
                    anchors.leftMargin: rectForm.width/3-width-14*dp
                     z:2
                }
                Image{
                  //  text:"密   码:"
                 //   font.pixelSize: dp*5
                    source:"qrc:/background/background/pwd.png"
                    width:10*dp;
                    height:10*dp
                   anchors.top:textpwd.top
                    anchors.left:rectForm.left
                   anchors.leftMargin: rectForm.width/3-width-14*dp
                     z:2
                }

        }//initialItem
    }//stackview


}
